<?php
namespace app1\index\model;

use think\Model;
use think\Image;

class User extends Model
{

    public $user;

    
    /**
     * 登录函数
     * @param string $user_name
     * @param string $user_password
     * @return number[]|string[]
     */
    public function login($user_name, $user_password, $identify)
    {
        if($user_name == null){
            return ["error"=>1, "info"=>"用户名为空，请填写用户名后再登录"];
        }
        
        if($user_password == null){
            return ["error"=>1, "info"=>"密码不能为空，请填写密码后再登录"];
        }
        
        $res=$this->where('user_name',$user_name)->find();
        if($res == null){
            return ["error"=>1, "info"=>'用户名不存在'];
        }
        
        if($res['user_password'] != $user_password){
            return ["error"=>1, "info"=>'用户名和密码不匹配'];  
        }
        
        if(!captcha_check($identify)){
            return ["error"=>1, "info"=>"验证码错误"];
        }
        
        session("user",$res);
        $image = $res['image']; 
        $thumb = $res['thumb'];
        return ["error"=>0, "info"=>'登录成功', "image"=>$image, "thumb"=>$thumb];
    }
    

   /**
    * 注册函数
    * @param string $user_name
    * @param string $user_password
    * @return number[]|string[]
    */
    public function regist($user_name, $user_password, $image, $thumb)
    {
        if($user_name == null){
            return ["error"=>1, "info"=>'用户名不能为空，请填写用户名'];
        }
        
        $pattern = '/[\/,\*,<>,\|,\?]/';
        if(preg_match($pattern, $user_name)) {
            return ["error"=>1,"info"=>'用户名不合法，请重新填写用户名'];
        }
        
        $res = $this->where('user_name',$user_name)->find();
        if($res) {
            return ["error"=>1,"info"=>'用户名已存在，请重新填写用户名'];
        }
        
        if($user_password == null){
            return ["error"=>1, "info"=>'密码不能为空，请填写密码'];
        }
        
        if(preg_match($pattern, $user_password)) {
            return ["error"=>1,"info"=>'密码不合法，请重新填写密码'];
        }
        
        if(strlen($user_password)<4){
            return ["error"=>1,"info"=>"密码不能少于四位"];
        }
        
        $this->data([
            'user_name' => $user_name,
            'user_password' => $user_password,
            'image'=>$image,
            'thumb'=>$thumb
        ]);
        
        $res = $this->save();
        
        return ["error"=>0, "info"=>'注册成功'];
        }
        
        public function upLoad($name){
            $path = ROOT_PATH.'/public';
            $imagepre = '/upLoads/image/';
            $logo = '/upLoads/image/logo/logo.png';
            $file = request()->file($name);
            $image = $file->move($path.$imagepre);
            $filename = $image->getSavename();
            
            $imagepath = $imagepre.$filename;
            $thumbpath = $imagepre.str_replace(['/','\\'], '/thum_', $filename);
            $waterpath = $imagepre.str_replace(['/','\\'], '/water_', $filename);
            Image::open($image)->thumb(50,50)->save($path.$thumbpath);
            Image::open($image)->water($path.$thumbpath,\think\Image::WATER_NORTHWEST)->save($path.$waterpath);
            $data = ['imagepath'=>$imagepath, 'thumbpath'=>$thumbpath, 'waterpath'=>$waterpath];
            return $data; 
        }
}